"""
file: test_dialog.py
Author: 城下秋草
Date: 2024/12/3
Desc: 弹出框及js处理
"""
import time

from playwright.sync_api import sync_playwright, expect

playwright = sync_playwright().start()
browser = playwright.chromium.launch(
    headless=False,
    slow_mo=500,
    args=["--window-position=1024,-850"]
)
page = browser.new_page()
page.goto("file:///C:/dev/qiucao/PlayWrightCourse/test.html")

def handle_alert(dialog):
    dialog.accept("城下秋草测试")

page.on('dialog',handle_alert)
alert_btn = page.get_by_role('button', name='Prompt')
alert_btn.click()
result_text = page.locator('#result').text_content()
print(result_text)

page.evaluate("document.getElementById('result').style.color='red'")
time.sleep(3)

page.locator('input[type]').set_input_files('test.html')
time.sleep(2)

page.on('filechooser',lambda file_chooser:file_chooser.set_files('test.java'))
page.locator('input[type]').click()
time.sleep(2)

expect(page.get_by_role('button')).to_have_count(3,timeout=1000)